package  
{
	/**
	 * ...
	 * @author Martina Rasch
	 */
	
	  import net.flashpunk.World;
	  import net.flashpunk.utils.Input;
	  import net.flashpunk.FP;
	  import net.flashpunk.utils.Data;
	  
	public class Highscore extends World
	{
		private var scores:Array;
		private var textBox:TextBox;
		private var players:Array;
		private var source:String = "game.cfg";
		private var newScore:int;
		
		private static var width:int;
		private static var height:int;
		
		public function Highscore(w:int, h:int, score:int = 0) 
		{
			width = w;
			height = h;
			add(new HighscoreBackground);
			Data.load("game.cfg");
			scores = new Array(Data.readInt("score1"), Data.readInt("score2"), Data.readInt("score3"), Data.readInt("score4"), Data.readInt("score5"));
			players = new Array(Data.readString("player1"), Data.readString("player2"), Data.readString("player3"), Data.readString("player4"), Data.readString("player5"));
			
			newScore = -1;
			
			for (var i:int; i < 5; i++) {
				if (score >= scores[i]) {
					newScore = i;
					for (var j:int = 4; j > -1; j--) {
						if (j > i) {
							scores[j] = scores[j - 1];
							players[j] = players[j-1];
						}
					}
					scores[i] = score;
					break;
				}
			}
			trace(scores);
			Data.writeInt("score1", scores[0]);
			Data.writeInt("score2", scores[1]);
			Data.writeInt("score3", scores[2]);
			Data.writeInt("score4", scores[3]);
			Data.writeInt("score5", scores[4]);
			
			for (var k:int = 0; k < 5; k++) {
				if (k != newScore) {
					add(new PlayerBar(100, 200 + k * 60));
					add(new ScoreBar(510, 200 + k * 60));
				} else {
					textBox = new TextBox(true, "Enter name...", 100, 200 + k * 60,400,50);
					add(new PlayerBar(100, 200 + k * 60, true));
					add(new ScoreBar(510, 200 + k * 60, true));
					add(textBox);
				}
			}
			
			add(new Scoretable(scores, players, score, newScore));
			
			//textBox = new TextBox(true, "Hallo", 100, 100,400,50);
			//add(textBox);
		}
		
		override public function update():void {
			super.update();
			
			if (Input.mousePressed) {
				FP.world = new Menu(width, height);
				//trace(textBox.text);
				
				if (newScore > -1) {
					players[newScore] = textBox.text;
				}
				
				Data.writeString("player1", players[0]);
				Data.writeString("player2", players[1]);
				Data.writeString("player3", players[2]);
				Data.writeString("player4", players[3]);
				Data.writeString("player5", players[4]);
				
				Data.save("game.cfg");
			}
		}
	}
	
}